<?php
/*
forecast.function.php
*/
session_start();
include_once("config/config.inc.php");
include_once("acctg/class.baseobject.php");
include_once("acctg/class.shift.php");

function insertfc($occupancy, $now, $receiptflag) {
	$shift = new shift(array());
	$thisshift = $shift->shiftno;
	$amount = getComputedAmount($occupancy);
	if ($receiptflag == 1) {
		$sql = "insert into forecast_collection (fc_date, shiftnum, occupancy_id, amount) 
			values ('$now', '$thisshift', '$occupancy','$amount') ";
	} else {
		$sql = "insert into tempfc (fc_date, shiftnum, occupancy_id, amount) 
			values ('$now', '$thisshift', '$occupancy','$amount') ";
	}
	mysql_query($sql) or die(mysql_error());
}

function getComputedAmount($occupancy) {
	$sql = "select sum(unit_cost*qty) from room_sales 
			where occupancy_id='$occupancy' and status='Paid' ";
	$res = mysql_query($sql);
	list($roomsales)=mysql_fetch_row($res);
	$sql = "select sum(unit_cost*qty) from fnb_sales 
			where occupancy_id='$occupancy' and status='Paid' ";
	$res = mysql_query($sql);
	list($fnbsales)=mysql_fetch_row($res);
	return $roomsales + $fnbsales;
}

function runfc($date='', $shift='') {
	if($date=='' && $shift=='') {
		$shift = new shift(array());
		$thisshift = $shift->shiftno;
		$now = date('Y-m-d');
	} else {
		$now = $date;
		$thisshift = $shift;
	}
	$limit = getLimit($now,$thisshift);
	$runningtotal = getRunningFC($now, $thisshift);
	while($limit > $runningtotal) {
		if(randomSelect($now, $thisshift) === false) break;
		$runningtotal = getRunningFC($now, $thisshift);
		if ($limit < $runningtotal) break; 
	}
}

function randomSelect($date, $shift) {
	$sql = "select * from tempfc where shiftnum='$shift' and date(fc_date)='$date' order by rand() limit 1";
	$res = mysql_query($sql);
	$num = mysql_num_rows($res);
	if ($num) {
		list($id, $fd,$fs,$occ, $fv)=mysql_fetch_row($res);
		$sql2 = "insert into forecast_collection(fc_date, shiftnum, occupancy_id, amount) values ('$fd','$fs','$occ','$fv')";
		mysql_query($sql2) or die(mysql_error() . $sql2);
		$sql3 = "delete from tempfc where fc_date='$fd' and shiftnum='$fs' and occupancy_id='$occ' and amount='$fv'";
		mysql_query($sql3) or die(mysql_error() . $sql3);
		return true;
	}
	return false;
}

function getLimit($date, $shift) {
	$sql = "select forecast_value from forecast where forecast_date='$date' and shiftnum='$shift'";
	$res = mysql_query($sql);
	list($limit) = mysql_fetch_row($res);
	return $limit;
}

function getRunningFC($now,$thisshift) {
	$sql  = "select sum(amount) from forecast_collection
			where date(fc_date)='$now' and shiftnum='$thisshift' ";
	$res = mysql_query($sql);
	list($amount)=mysql_fetch_row($res) or die($sql);
	return $amount;
}

function getForecastList($date, $shift) {
	$sql = "select * from forecast_collection 
			where fc_date='$date' and shiftnum='$shift' ";
	$res = mysql_query($sql);
	$num = mysql_num_rows($res);
	if($num) {
		$retval="$num records found.<br>";
		$retval.="<table>";
		$retval.="<tr>";
			$retval.="<th>Occupancy</th>";
			$retval.="<th>Amount</th>";
			$retval.="</tr>";
		while(list($id, $fd, $fs, $occ, $amt)=mysql_fetch_row($res)) {
			$retval.="<tr>";
			$retval.="<td>$occ</td>";
			$retval.="<td class='amt'>$amt</td>";
			$retval.="</tr>";
			$totalamt += $amt;
		}
		$totalamt = number_format($totalamt, 2);
		$retval.="<tr><th>Total:</th><th class='amt'>$totalamt</th></tr>";
		$retval.="</table>";
	} else {
		$retval = "no data found";
	}
	return $retval;
}

function getForecastManager($date, $shift) {
	$sql = "select * from forecast_collection 
			where fc_date='$date' and shiftnum='$shift' ";
	$res = mysql_query($sql);
	$num = mysql_num_rows($res);
	$target = getLimit($date, $shift);
	$target = number_format($target,2);
	if($num) {
		//$retval="$num records found.<br>";
		$retval.="<table>";
		$retval.="<tr>";
		$retval.="<th><input type='checkbox' name='cball' id='cball' class='cball' /></th>";
		$retval.="<th>Occupancy</th>";
		$retval.="<th>Amount</th>";
		$retval.="</tr>";
		
		while(list($id, $fd, $fs, $occ, $amt)=mysql_fetch_row($res)) {
			$retval.="<tr>";
			$retval.="<th><input type='checkbox' name='cball' id='cball' class='cball' /></th>";	
			$retval.="<td>$occ</td>";
			$retval.="<td class='amt'>$amt</td>";
			$retval.="</tr>";
			$totalamt += $amt;
		}
		$totalamt = number_format($totalamt, 2);
		$retval.="<tr><th colspan='2' class='amt'>Total:</th><th class='amt'>$totalamt</th></tr>";
		$retval.="<tr><th colspan='2' class='amt'>Target:</th><th class='amt'>$target</th></tr>";
		$retval.="</table>";
	} else {
		$retval = "no data found";
	}
	return $retval;
}


function getTempForecastManager($date, $shift) {
	$sql = "select * from tempfc 
			where date(fc_date)='$date' and shiftnum='$shift' ";
	$res = mysql_query($sql);
	$num = mysql_num_rows($res);
	if($num) {
		//$retval="$num records found.<br>";
		$retval.="<table>";
		$retval.="<tr>";
			$retval.="<th>Date</th>";
			$retval.="<th>Occupancy</th>";
			$retval.="<th>Amount</th>";
			$retval.="</tr>";
		while(list($id, $fd, $fs, $occ, $amt)=mysql_fetch_row($res)) {
			$retval.="<tr>";
			$retval.="<td>$fd</td>";
			$retval.="<td>$occ</td>";
			$retval.="<td class='amt'>$amt</td>";
			$retval.="</tr>";
			$totalamt += $amt;
		}
		$totalamt = number_format($totalamt, 2);
		$retval.="<tr><th colspan='2' class='amt'>Total:</th><th class='amt'>$totalamt</th></tr>";
		$retval.="</table>";
	} else {
		$retval = "no data found";
	}
	return $retval;
}
?>